﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Zzva.Common;
using System.Data;
using Zzva.DomainObject; 
using System.Collections.ObjectModel;
using System.Collections;

namespace Zzva.ESB
{

    public class TEsb:TEsbDomainObject 
    {

        private static TEsb mvarEsb;

        # region Software переопределяем всвязи с вводом связи SoftType

        public Collection<TDirSoftware> GetListSoftware(int pForOverLoad)
        {
            try
            {
                DataTable lListSoftware;
                Collection<TDirSoftware> lCollSoftware;
                int lId;
                TDirSoftware lDirSoftware;

                lListSoftware = SysMrpZzva.GetListSoftware();
                lCollSoftware = new Collection<TDirSoftware>();

                if (lListSoftware.Rows.Count != 0)
                {
                    foreach (DataRow row in lListSoftware.Rows)
                    {
                        lId = (int)row["Id"];
                        lDirSoftware = GetSoftware(lId,0);
                        lCollSoftware.Add(lDirSoftware);
                    }
                }
                else { }

                return lCollSoftware;
            }

            catch (Exception e) { throw e; }
            finally { }
        }

        public void SaveSoftware(ref TDirSoftwareBase pDirSoftwareBase, int pForOverLoad)
        {
            try
            {
                Type lTypeObject;

                lTypeObject = pDirSoftwareBase.GetType();

                switch (lTypeObject.Name)
                {
                    case "TDirSoftware":
                        TDirSoftware lDirSoftware = (TDirSoftware)pDirSoftwareBase;
                        EditSoftware(ref lDirSoftware, 0);
                        break;
                    case "TDirSoftwareNew":
                        AddSoftware(ref pDirSoftwareBase,0);
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private void AddSoftware(ref TDirSoftwareBase pDirSoftwareBase, int pForOverLoad)
        {
            try
            {
                int lId;
                string lObozn;
                string lNaim;
                int lHolderId;
                int lDeveloperId;
                TDirITCompanyBase lHolder;
                TDirITCompanyBase lDeveloper;
                Type lTypeObject;

                int lSoftTypeId;
                TDirSoftTypeBase lSoftType;



                lObozn = pDirSoftwareBase.Obozn;
                lNaim = pDirSoftwareBase.Naim;

                lHolder = pDirSoftwareBase.Holder;
                lTypeObject = lHolder.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        lHolderId = ((TDirITCompany)pDirSoftwareBase.Holder).Id;
                        break;
                    case "TDirITCompanyNull":
                        lHolderId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }


                lDeveloper = pDirSoftwareBase.Developer;
                lTypeObject = lDeveloper.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        lDeveloperId = ((TDirITCompany)pDirSoftwareBase.Developer).Id;
                        break;
                    case "TDirITCompanyNull":
                        lDeveloperId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }



                lSoftType = pDirSoftwareBase.SoftType;
                lTypeObject = lSoftType.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirSoftType":
                        lSoftTypeId = ((TDirSoftType)pDirSoftwareBase.SoftType).Id;
                        break;
                    case "TDirSoftTypeNull":
                        lSoftTypeId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа SoftType"));
                    //break;
                }

                lId = SysMrpZzva.AddSoftware(lObozn, lNaim, lHolderId, lDeveloperId,lSoftTypeId);
                pDirSoftwareBase = this.GetSoftware(lId,0);

            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private void EditSoftware(ref TDirSoftware pDirSoftware, int pForOverLoad)
        {
            try
            {
                int lId;
                string lObozn;
                string lNaim;
                int lHolderId;
                int lDeveloperId;
                TDirITCompanyBase lHolder;
                TDirITCompanyBase lDeveloper;
                Type lTypeObject;

                int lSoftTypeId;
                TDirSoftTypeBase lSoftType;



                lId = pDirSoftware.Id;
                lObozn = pDirSoftware.Obozn;
                lNaim = pDirSoftware.Naim;


                lHolder = pDirSoftware.Holder;
                lTypeObject = lHolder.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        lHolderId = ((TDirITCompany)pDirSoftware.Holder).Id;
                        break;
                    case "TDirITCompanyNull":
                        lHolderId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }


                lDeveloper = pDirSoftware.Developer;
                lTypeObject = lDeveloper.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        lDeveloperId = ((TDirITCompany)pDirSoftware.Developer).Id;
                        break;
                    case "TDirITCompanyNull":
                        lDeveloperId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }


                lSoftType = pDirSoftware.SoftType;
                lTypeObject = lSoftType.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirSoftType":
                        lSoftTypeId = ((TDirSoftType)pDirSoftware.SoftType).Id;
                        break;
                    case "TDirSoftTypeNull":
                        lSoftTypeId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа SoftType"));
                    //break;
                }

                SysMrpZzva.EditSoftware(lId, lObozn, lNaim, lDeveloperId, lHolderId,lSoftTypeId);
                pDirSoftware = this.GetSoftware(lId,0);

            }
            catch (Exception e) { throw e; }
            finally { }
        }

        public TDirSoftware GetSoftware(int pId, int pForOverload)
        {
            try
            {
                TDirSoftware lDirSoftware;
                string lObozn;
                string lNaim;
                int lHolder;
                int lDeveloper;
                int lSoftType;

                if (SysMrpZzva.GetSoftware(pId, out lObozn, out lNaim, out lHolder, out lDeveloper,out lSoftType) == true)
                {
                    return lDirSoftware = new TDirSoftware(this, pId, lObozn, lNaim, lHolder, lDeveloper,lSoftType);
                }
                else{throw (new CommonException("Software ИД =  " + pId + " отсутсвует"));}

            }
            catch (Exception e) { throw e; }
            finally { }
        }

        # endregion


        # region SoftType

        public override TDirSoftType GetSoftType(int pId)
        {
            try
            {
                TDirSoftType lDirSoftType;
                string lObozn;
                string lNaim;

                if (SysMrpZzva.GetSoftType(pId, out lObozn, out lNaim) == true)
                {
                    return lDirSoftType = new TDirSoftType(this, pId, lObozn, lNaim);
                }
                else
                {
                    throw (new CommonException("SoftType ИД =  " + pId + " отсутсвует"));
                }
            }

            catch (Exception e) { throw e; }
            finally { }
        }

        public void SaveSoftType(ref TDirSoftTypeBase pDirSoftType)
        {
            try
            {
                Type lTypeObject;

                lTypeObject = pDirSoftType.GetType();

                switch (lTypeObject.Name)
                {
                    case "TDirSoftType":
                        TDirSoftType lDirSoftType = (TDirSoftType)pDirSoftType;

                        EditSoftType(ref lDirSoftType);
                        break;
                    case "TDirSoftTypeNew":
                        //TDirSoftTypeNew lDirSoftTypeNew = (TDirSoftTypeNew)pDirSoftType;
                        //AddSoftType(ref lDirSoftTypeNew);
                        AddSoftType(ref pDirSoftType);
                        break;
                    default:
                        throw (new CommonException("Нет типа DirSoftType"));
                    //break;
                }
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private void EditSoftType(ref TDirSoftType pDirSoftType)
        {
            try
            {
                int lId;
                string lObozn;
                string lNaim;

                lId = pDirSoftType.Id;
                lObozn = pDirSoftType.Obozn;
                lNaim = pDirSoftType.Naim;


                SysMrpZzva.EditSoftType(lId, lObozn, lNaim);

                pDirSoftType = this.GetSoftType(lId);


            }
            catch (Exception e) { throw e; }
            finally { }
        }

        public void DeleteSoftType(ref  TDirSoftTypeBase pDirSoftType)
        {
            try
            {
                Type lTypeObject;
                lTypeObject = pDirSoftType.GetType();


                if (lTypeObject.Name == "TDirSoftType")
                {
                    TDirSoftType lDirSoftType = (TDirSoftType)pDirSoftType;

                    if (CheckRelationSoftType(lDirSoftType) == false)
                    {

                        SysMrpZzva.DeleteSoftType(lDirSoftType.Id);
                        pDirSoftType = new TDirSoftTypeNull();
                    }
                    else { throw (new CommonException("Не удаляется! Существуют связи этой SoftType с другими объектами")); }
                }
                else { throw (new CommonException("Только постоянный объект можно удалять")); }
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private bool CheckRelationSoftType(TDirSoftType pDirSoftType)
        {

            try
            {
                int lId;
                bool Result;

                lId = pDirSoftType.Id;

                if (SysMrpZzva.CheckRelationSoftType(lId) == false)
                //& (SysKis.CheckRelationSoftType(lId) == false)//проверяется по всем физическим системам в котроых есть связи
                { Result = false; }
                else { Result = true; }

                return Result;

            }

            catch (Exception e) { throw e; }
            finally { }

        }

        public Collection<TDirSoftType> GetListSoftType()
        {
            try
            {
                DataTable lListSoftType;
                Collection<TDirSoftType> lCollSoftType;
                int lId;
                TDirSoftType lDirSoftType;

                lListSoftType = SysMrpZzva.GetListSoftType();
                lCollSoftType = new Collection<TDirSoftType>();

                if (lListSoftType.Rows.Count != 0)
                {
                    foreach (DataRow row in lListSoftType.Rows)
                    {
                        lId = (int)row["Id"];
                        lDirSoftType = GetSoftType(lId);
                        lCollSoftType.Add(lDirSoftType);
                    }
                }
                else { }

                return lCollSoftType;
            }

            catch (Exception e) { throw e; }
            finally { }
        }

        private void AddSoftType(ref TDirSoftTypeBase pDirSoftType)
        {
            try
            {
                int lId;
                string lObozn;
                string lNaim;

                lObozn = pDirSoftType.Obozn;
                lNaim = pDirSoftType.Naim;

                lId = SysMrpZzva.AddSoftType(lObozn, lNaim);

                pDirSoftType = this.GetSoftType(lId);

            }
            catch (Exception e) { throw e; }
            finally { }
        }

        # endregion







































        //Вернуть последний код ИД Software
        public int GetLastIdSoftware()
        {
            try
            {
                return SysMrpZzva.GetLastIdSoftware();  
            }
            catch (Exception e) { throw e; }
            finally { }
        }












        public Collection<TDirSoftware> GetListSoftware()
        {
            try
            {
                DataTable lListSoftware;
                Collection<TDirSoftware> lCollSoftware;
                int lId;
                TDirSoftware lDirSoftware;

                lListSoftware = SysMrpZzva.GetListSoftware();
                lCollSoftware = new Collection<TDirSoftware>();

                if (lListSoftware.Rows.Count != 0)
                {
                    foreach (DataRow row in lListSoftware.Rows)
                    {
                        lId = (int)row["Id"];
                        lDirSoftware = GetSoftware(lId);
                        lCollSoftware.Add(lDirSoftware);
                    }
                }
                else { }

                return lCollSoftware;
            }

            catch (Exception e) { throw e; }
            finally { }
        }




















































        #region {Constructors}
        protected TEsb() { }
        #endregion



        #region {DirITCompany}

 


//''        private void AddITCompany(ref TDirITCompanyBase pDirITCompany)
//''        {
//''            try
//''            {
//''                int lId;
//''                string lObozn;
//''                string lNaim;
//''
//''                lObozn = pDirITCompany.Obozn;
//''                lNaim = pDirITCompany.Naim;
//''
//''
//''                lId = SysMrpZzva.AddITCompany(lObozn, lNaim);
//''
//''                pDirITCompany = this.GetITCompany(lId);
//''
//''
//''            }
//''            catch (Exception e) { throw e; }
//''            finally { }
//''        }


        private void AddITCompany(ref TDirITCompanyBase pDirITCompany)
        {
            try
            {
                int lId;
                string lObozn;
                string lNaim;

                lObozn = pDirITCompany.Obozn;
                lNaim = pDirITCompany.Naim;


                lId = SysMrpZzva.AddITCompany(lObozn, lNaim);

                pDirITCompany = this.GetITCompany(lId);


            }
            catch (Exception e) { throw e; }
            finally { }
        }

        public Collection<TDirITCompany> GetListITCompany()
        {
            try
            {
                DataTable lListITCompany;
                Collection<TDirITCompany> lCollITCompany;
                int lId;
                TDirITCompany lDirITCompany;

                lListITCompany = SysMrpZzva.GetListITCompany();
                lCollITCompany = new Collection<TDirITCompany>();

                if (lListITCompany.Rows.Count != 0)
                {
                    foreach (DataRow row in lListITCompany.Rows)
                    {
                        lId = (int)row["Id"];
                        lDirITCompany = GetITCompany(lId);
                        lCollITCompany.Add(lDirITCompany);
                    }
                }
                else{}

                return lCollITCompany;
            }

            catch (Exception e) { throw e; }
            finally { }
        }

        public void DeleteITCompany(ref  TDirITCompanyBase pDirITCompany)
        {
            try
            {
                Type lTypeObject;
                lTypeObject = pDirITCompany.GetType();


                if (lTypeObject.Name == "TDirITCompany")
                {
                    TDirITCompany lDirITCompany = (TDirITCompany)pDirITCompany;

                    if (CheckRelationITCompany(lDirITCompany) == false)
                    {

                        SysMrpZzva.DeleteITCompany(lDirITCompany.Id);
                        pDirITCompany = new TDirITCompanyNull();
                    }
                    else { throw (new CommonException("Не удаляется! Существуют связи этой ITCompany с другими объектами")); }
                }
                else { throw (new CommonException("Только постоянный объект можно удалять")); }
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private bool CheckRelationITCompany(TDirITCompany pDirITCompany)
        {

            try
            {
                int lId;
                bool Result;

                lId = pDirITCompany.Id;

                if (SysMrpZzva.CheckRelationITCompany(lId) == false)
                //& (SysKis.CheckRelationITCompany(lId) == false)//проверяется по всем физическим системам в котроых есть связи
                { Result = false; }
                else { Result = true; }

                return Result;

            }

            catch (Exception e) { throw e; }
            finally { }

        }

 
        private void EditITCompany(ref TDirITCompany pDirITCompany)
        {
            try
            {
                int lId;
                string lObozn;
                string lNaim;

                lId = pDirITCompany.Id;
                lObozn = pDirITCompany.Obozn;
                lNaim = pDirITCompany.Naim;


                SysMrpZzva.EditITCompany(lId, lObozn, lNaim);

                pDirITCompany = this.GetITCompany(lId);


            }
            catch (Exception e) { throw e; }
            finally { }
        }

        public void SaveITCompany(ref TDirITCompanyBase pDirITCompany)
        {
            try
            {
                Type lTypeObject;

                lTypeObject = pDirITCompany.GetType();

                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        TDirITCompany lDirITCompany = (TDirITCompany)pDirITCompany;
                        
                        EditITCompany(ref lDirITCompany);
                        break;
                    case "TDirITCompanyNew":
                        //TDirITCompanyNew lDirITCompanyNew = (TDirITCompanyNew)pDirITCompany;
                        //AddITCompany(ref lDirITCompanyNew);
                        AddITCompany(ref pDirITCompany);
                        break;
                    default:
                        throw (new CommonException("Нет типа DirITCompany"));
                    //break;
                }
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        public override TDirITCompany GetITCompany(int pId)
        {
            try
            {
                TDirITCompany lDirITCompany;
                string lObozn;
                string lNaim;

                if (SysMrpZzva.GetITCompany(pId, out lObozn, out lNaim) == true)
                {
                    return lDirITCompany = new TDirITCompany(this, pId, lObozn, lNaim);
                }
                else
                {
                    throw (new CommonException("ITCompany ИД =  " + pId + " отсутсвует"));
                }
            }

            catch (Exception e) { throw e; }
            finally { }
        }

        #endregion







        #region {DirSoftware}

        public void DeleteSoftware(ref  TDirSoftwareBase pDirSoftwareBase)
        {
            try
            {
                Type lTypeObject;
                lTypeObject = pDirSoftwareBase.GetType();


                if (lTypeObject.Name == "TDirSoftware")
                {
                    TDirSoftware lDirSoftware = (TDirSoftware)pDirSoftwareBase;

                    if (CheckRelationSoftware(lDirSoftware) == false)
                    {

                        SysMrpZzva.DeleteSoftware(lDirSoftware.Id);
                        pDirSoftwareBase = new TDirSoftwareNull();
                    }
                    else { throw (new CommonException("Не удаляется! Существуют связи этой Software с другими объектами")); }
                }
                else { throw (new CommonException("Только постоянный объект можно удалять")); }
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private bool CheckRelationSoftware(TDirSoftware pDirSoftware)
        {
            try
            {
                bool Result;



                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //Заглушка до момента появления связей
                Result = false;
                return Result;
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private void AddSoftware(ref TDirSoftwareBase pDirSoftwareBase)
        {
            try
            {
                int lId;
                string lObozn;
                string lNaim;
                int lHolderId;
                int lDeveloperId;
                TDirITCompanyBase lHolder;
                TDirITCompanyBase lDeveloper;
                Type lTypeObject;


                lObozn = pDirSoftwareBase.Obozn;
                lNaim = pDirSoftwareBase.Naim;

                lHolder = pDirSoftwareBase.Holder;
                lTypeObject = lHolder.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        lHolderId = ((TDirITCompany)pDirSoftwareBase.Holder).Id;
                        break;
                    case "TDirITCompanyNull":
                        lHolderId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }


                lDeveloper = pDirSoftwareBase.Developer;
                lTypeObject = lDeveloper.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        lDeveloperId = ((TDirITCompany)pDirSoftwareBase.Developer).Id;
                        break;
                    case "TDirITCompanyNull":
                        lDeveloperId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }


                lId = SysMrpZzva.AddSoftware(lObozn, lNaim,lHolderId,lDeveloperId);

                pDirSoftwareBase = this.GetSoftware(lId);


            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private void EditSoftware(ref TDirSoftware pDirSoftware)
        {
            try
            {
                int lId;
                string lObozn;
                string lNaim;
                int lHolderId;
                int lDeveloperId;
                TDirITCompanyBase lHolder;
                TDirITCompanyBase lDeveloper;
                Type lTypeObject;


                lId = pDirSoftware.Id;
                lObozn = pDirSoftware.Obozn;
                lNaim = pDirSoftware.Naim;


                lHolder = pDirSoftware.Holder;
                lTypeObject = lHolder.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        lHolderId = ((TDirITCompany)pDirSoftware.Holder).Id; 
                        break;
                    case "TDirITCompanyNull":
                        lHolderId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }


                lDeveloper = pDirSoftware.Developer;
                lTypeObject = lDeveloper.GetType();
                switch (lTypeObject.Name)
                {
                    case "TDirITCompany":
                        lDeveloperId = ((TDirITCompany)pDirSoftware.Developer).Id;
                        break;
                    case "TDirITCompanyNull":
                        lDeveloperId = 0;
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }

                SysMrpZzva.EditSoftware(lId, lObozn, lNaim,lDeveloperId,lHolderId);

                pDirSoftware = this.GetSoftware(lId);


            }
            catch (Exception e) { throw e; }
            finally { }
        }

        public void SaveSoftware(ref TDirSoftwareBase pDirSoftwareBase)
        {
            try
            {
                Type lTypeObject;

                lTypeObject = pDirSoftwareBase.GetType();

                switch (lTypeObject.Name)
                {
                    case "TDirSoftware":
                        TDirSoftware lDirSoftware = (TDirSoftware)pDirSoftwareBase;
                        EditSoftware(ref lDirSoftware);
                        break;
                    case "TDirSoftwareNew":
                        AddSoftware(ref pDirSoftwareBase);
                        break;
                    default:
                        throw (new CommonException("Нет типа Software"));
                    //break;
                }
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        public TDirSoftware GetSoftware(int pId)
        {
            try
            {
                TDirSoftware lDirSoftware;
                string lObozn;
                string lNaim;
                int lHolder;
                int lDeveloper;


                if (SysMrpZzva.GetSoftware(pId, out lObozn, out lNaim, out lHolder, out lDeveloper) == true)
                {
                    return lDirSoftware = new TDirSoftware(this, pId, lObozn, lNaim, lHolder, lDeveloper);
                }
                else
                {
                    throw (new CommonException("Software ИД =  " + pId + " отсутсвует"));
                }



            }

            catch (Exception e) { throw e; }
            finally { }
        }

        #endregion











        #region {Systems}
        public static TEsb GetEsb()
        {
            try
            {
                if (mvarEsb == null)
                {
                    mvarEsb = new TEsb();
                }
                return mvarEsb;
            }
            catch (Exception e) { throw e; }
            finally { }
        }

        private TSysKis SysKis
        {
            get
            {
                try
                {
                    return TSysKis.GetSystem();
                }
                catch (Exception e) { throw e; }
                finally { }
            }
        }

        private TSysMrpZzva SysMrpZzva
        {
            get
            {
                try
                {
                    return TSysMrpZzva.GetSystem();
                }
                catch (Exception e) { throw e; }
                finally { }
            }
        }

        private TSysBko SysBko
        {
            get
            {
                try
                {
                    return TSysBko.GetSystem();
                }
                catch (Exception e) { throw e; }
                finally { }
            }
        }

        private TSysBuhuch SysBuhuch
        {
            get
            {
                try
                {
                    return TSysBuhuch.GetSystem();
                }
                catch (Exception e) { throw e; }
                finally { }
            }
        }

        private TSysWorkprogress SysWorkprogress
        {
            get
            {
                try
                {
                    return TSysWorkprogress.GetSystem();
                }
                catch (Exception e) { throw e; }
                finally { }
            }
        }
        #endregion































        #region {TestFunction}

        public DataTable TestGetListMater()
        {
            try
            {

                return SysKis.TestGetListMater();

            }

            catch (Exception e) { throw e; }
            finally { }
        }

        public string TestConnectSysMrpZzva()
        {
            return SysMrpZzva.TestConnect();
        }

        public string TestConnectSysWorkprogress()
        {
            return SysWorkprogress.TestConnect() ;
        }

        public string TestGetNetNamePk(int Pk)
        {
            return SysBko.TestGetNetNamePk(Pk);
        }

        public bool TestGetNomencl(string pObozn, out string pNaim, out string pEi)
        {
            try
            {

                return SysBuhuch.GetNomencl(pObozn, out pNaim, out pEi);

            }

            catch (Exception e) { throw e; }
            finally { }
        }

        #endregion



    }




}
